package com.xtianzhuang.www.controller.project;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VProjectTask;
import com.xtianzhuang.www.entity.VProjectTaskExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.AddTaskRequest;
import com.xtianzhuang.www.entity.request.DeleteTaskRequest;
import com.xtianzhuang.www.entity.request.TaskListQueryRequest;
import com.xtianzhuang.www.entity.request.UpdateTaskRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.AddTaskResponse;
import com.xtianzhuang.www.entity.response.DeleteTaskResponse;
import com.xtianzhuang.www.entity.response.TaskListQueryResponse;
import com.xtianzhuang.www.entity.response.UpdateTaskResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;
import com.xtianzhuang.www.utils.PageData;

/**
 * 
 * @author xtian.zhuang
 * @date Jul 22, 2019
 */
@Controller
@RequestMapping("/project")
public class ProjectTaskController extends BaseController {
	@RequestMapping(value = "/task-list")
	public ModelAndView taskList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("project/task-list");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping("/ajaxAddTask")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public AddTaskResponse ajaxAddTask(AddTaskRequest request) {
		return projectServiceBiz.addTask(request);
	}

	@RequestMapping("/ajaxUpdateTask")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public UpdateTaskResponse ajaxUpdateTask(UpdateTaskRequest request) {
		return projectServiceBiz.updateTask(request);
	}

	@RequestMapping("/ajaxQuerytaskList")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public TaskListQueryResponse querytaskList(TaskListQueryRequest request) {
		TaskListQueryResponse response = new TaskListQueryResponse();
		VProjectTaskExample example = new VProjectTaskExample();
		VProjectTaskExample.Criteria criteria = example.createCriteria();
		if (request.getProjectId() != null) {
			criteria.andProjectIdEqualTo(request.getProjectId());
		}
		if (StringUtils.isNotEmpty(request.getTaskState())) {
			criteria.andStateEqualTo(request.getTaskState());
		}
		if (StringUtils.isNotEmpty(request.getTaskType())) {
			criteria.andTaskTypeEqualTo(request.getTaskType());
		}
		example.setOffset(request.getOffset());
		example.setLimit(request.getLimit());
		PageData<VProjectTask> pageData = commonServiceBiz.getPageData(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setData(pageData.getData());
		response.setTotal(pageData.getTotal());
		return response;
	}

	@RequestMapping("/ajaxDeleteTask")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public DeleteTaskResponse deleteTask(DeleteTaskRequest request) {
		DeleteTaskResponse response = new DeleteTaskResponse();
		projectServiceBiz.deleteProjectTask(request);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("删除成功");
		return response;
	}

}
